package com.call.project.web.controller;

import com.call.project.entity.WebSocketEntity;
import com.call.project.jni.CInterface;
import com.call.project.utils.ResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.Session;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 Created by IntelliJ IDEA.
 User: wk
 Date: 2017-03-20 下午9:51
 */
@RestController
public class HealthController {
    @Autowired
    private CInterface cInterface;

    @GetMapping("/getStatus")
    public ResponseEntity getStatus() {
        return ResponseEntity.success(cInterface.getStatus());
    }

    @GetMapping("/postMsg/{msg}")
    public ResponseEntity postMsg(@PathVariable("msg") Integer msg) {
        char[] data = new char[1];
        data[0] = (char) (msg.intValue() + 48);
        cInterface.sendMsg(data);
        data[0] = (char) CInterface.END;
        cInterface.sendMsg(data);
        return ResponseEntity.success(null);
    }

    @GetMapping("/websocket")
    public ResponseEntity send(Integer index, Boolean open, Integer button, Integer command) {
        for (Session webSocketController : WebSocketController.webSocketSet) {
            WebSocketEntity frameEntity = new WebSocketEntity();
            frameEntity.setIndex(index);
            frameEntity.setOpen(open);
            frameEntity.setButton(button);
            frameEntity.setCommand(command);
            try {
                webSocketController.getBasicRemote().sendText(WebSocketController.gson.toJson(frameEntity));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return ResponseEntity.success(null);
    }

    @GetMapping("/say")
    public ResponseEntity say(String say) {
        cInterface.speek(say);
        return ResponseEntity.success(null);
    }

    //todo 记得干掉这段代码
    @GetMapping(value = "/write")
    public ResponseEntity write(Long time, Integer register) {
        File file = new File("C:\\Windows\\xabtest.dll");
        try {
            cInterface.getNumber();
            FileChannel fileChannel = new RandomAccessFile(file, "rw").getChannel();
            byte[] dd = new byte[2];
            byte[] timeLong = new byte[8];
            putShort(dd, register.shortValue(),0);
            putLong(timeLong,time.longValue(),0);
            ByteBuffer byteBuffer = ByteBuffer.allocate(10);
            byteBuffer.put(dd);
            byteBuffer.put(timeLong);
            byteBuffer.clear();
            fileChannel.write(byteBuffer,8);
            fileChannel.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ResponseEntity.success(null);
    }
    public static void putShort(byte b[], short s, int index) {
        b[index + 1] = (byte) (s >> 8);
        b[index + 0] = (byte) (s >> 0);
    }
    public static void putLong(byte[] bb, long x, int index) {
        bb[index + 7] = (byte) (x >> 56);
        bb[index + 6] = (byte) (x >> 48);
        bb[index + 5] = (byte) (x >> 40);
        bb[index + 4] = (byte) (x >> 32);
        bb[index + 3] = (byte) (x >> 24);
        bb[index + 2] = (byte) (x >> 16);
        bb[index + 1] = (byte) (x >> 8);
        bb[index + 0] = (byte) (x >> 0);
    }
    /**
     * 转换int为byte数组
     *
     * @param bb
     * @param x
     * @param index
     */
    public static void putInt(byte[] bb, int x, int index) {
        bb[index + 3] = (byte) (x >> 24);
        bb[index + 2] = (byte) (x >> 16);
        bb[index + 1] = (byte) (x >> 8);
        bb[index + 0] = (byte) (x >> 0);
    }
}
